<?php

namespace App\Http\Controllers;

use App\Chest;
use App\Order;
use App\Package;
use App\ReFund;
use App\RefundType;
use App\WithDraWal;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class WalletController extends Controller
{

    public function withdraw(Request $request)
    {
        $user = Auth::user();
        $canFee = $request->get('fee');
        return view('device.withdraw', compact('user', 'canFee'));
    }

    public function withdrawAudit(Request $request)
    {
        $user = Auth::user();
        $request = $request->all();
        $withdraw = WithDraWal::create([
            'user_id' => $user->id,
            'amount' => $request['fee'],
        ]);
        if ($withdraw) {
            $oldCanFee = $user->fee()->first();
            $canFee = $oldCanFee['can_fee'] - $request['fee'];
            $user->fee()->update(['can_fee' => $canFee]);
            return $this->message('您的提现申请已提交成功', route('user.index'));
        } else {
            return $this->message('您的提现申请已提交成功', route('user.index'));
        }
    }

    public function records()
    {
        // 收益记录 to do
        $user = Auth::user();
        $Lists = $user->drawal()->where(['state' => '1'])->get();
        $incomes = $user->income()->get();
        return view('device.incomeRecords', compact('user', 'Lists', 'incomes'));
    }


    public function showPackageForm($id)
    {

        $user = Auth::user();
        $group = $user->group()->find($id);
        if (!$group) {
            return $this->error('柜子不存在');
        }
        $chestId = $group->getSlug('chest');
        $chest = Chest::find($chestId);
        $chestType = $chest->type->find($chest->type_id);
        $packages = Package::whereIn('id', $chestType->package_id)->get();
        return view('wallet.renewal', compact('group', 'packages', 'chestId'));
    }

    public function renewal(Request $request, $id)
    {
        $this->validate($request,[
            'package_id' => 'required|integer|min:1',
            'chest_id' => 'required|integer|min:1'
        ]);

        $user = Auth::user();
        $group = $user->group()->find($id);
        if (!$group) {
            return $this->error('柜子不存在');
        }

        $packageId = $request->get('package_id');
        $package = Package::find($packageId);
        $addition = $request->get('chest_id');
        $order = Order::generate($user, $package->money, $package->id, get_class($package), $addition);
        return route('counter.wechat', ['order_no' => $order->order_no]);
    }


    public function deposit(Request $request)
    {
        $user = Auth::user();
        $gripId = $request->get('gripId');
        $chestId = $request->get('chestId');
        $chest = Chest::where(['id'=>$chestId])->first();
        return view('wallet.deposit', compact('user','chest','gripId'));
    }

    public function showRefundPage($id)
    {
        $user = Auth::user();
        $group = $user->group->find($id);
        $types = RefundType::get();

        $view = 'wallet.refund';
        if ($group->isRefunding()) {
            $view = 'wallet.audit';
        }
        return view($view, compact('user', 'types', 'group'));
    }


    public function refund(Request $request, $id)
    {
        $user = Auth::user();
        $group = $user->group()->find($id);

        if (!$group) {
            return $this->error('权限不足');
        }

        $route = route('user.device');
        $gripId = $group->getSlug('grip');
        $refund = $user->refund()->where([
            'grip_id' => $gripId,
            'state' => 0
        ])->first();

        if ($refund) {
            return $this->message('您已经提交过退款申请，不可以重复提交哟', $route);
        } else {
            $chestId = $group->getSlug('chest');
            $chest = Chest::find($chestId);
            $data = $request->only([
                'refund_desc', 'type_id'
            ]);
            $data['amount'] = $chest->deposit;
            $data['chest_id'] = $chest->id;
            $data['grip_id'] = $gripId;
            $data['state'] = ReFund::USABLE;
            $refund = $user->refund()->create($data);

            if ($refund) {
                $group->refund();
                return $this->success('您的退款申请已提交成功', $route);
            } else {
                return $this->error('系统异常', $route);
            }
        }
    }


    public function cancelRefund($id)
    {
        $user = Auth::user();
        $group = $user->group()->find($id);
        $gripId = $group->getSlug('grip');

        $refund = $user->refund()
            ->where([
                'grip_id' => $gripId
            ])
            ->first();
        if (!$refund->isUsable()) {
            return $this->message('您所撤销的申请不存在，请核实', route('user.device'));
        }

        $flag = DB::transaction(function () use ($refund, $group) {
            $refund->cancel();
            $group->cancel();
        });

        if ($flag) {
            return $this->success('撤销申请成功', route('user.device'));
        }else{
            return $this->error('系统异常', route('user.device'));
        }
    }


}





















